let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    nav_select: false, // 快捷导航

    name: '',
    province: '',
    city: '',
    region: '',
    phone: '',
    detail: '',
    error: '',
    address: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 表单提交
   */
  saveData: function (e) {
    let _this = this,
      values = e.detail.value
    values.region = _this.data.region;
    values.province = _this.data.province;
    values.city = _this.data.city;
    // 记录formId
    // App.saveFormId(e.detail.formId);

    // 表单验证
    if (!_this.validation(values)) {
      App.showError(_this.data.error);
      return false;
    }

    // 按钮禁用
    _this.setData({
      disabled: true
    });

    // 提交到后端
    App._post_form('wxapi/address/add', values, function (result) {
      App.showSuccess(result.message, function () {
        wx.navigateBack();
      });
    }, false, function () {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },

  /**
   * 表单验证
   */
  validation: function (values) {
    if (values.name === '') {
      this.data.error = '收件人不能为空';
      return false;
    }
    if (values.phone.length < 1) {
      this.data.error = '手机号不能为空';
      return false;
    }
    if (values.phone.length !== 11) {
      this.data.error = '手机号长度有误';
      return false;
    }

    if (!this.data.region) {
      this.data.error = '省市区不能空';
      return false;
    }
    if (values.detail === '') {
      this.data.error = '详细地址不能为空';
      return false;
    }
    return true;
  },

  /**
   * 修改地区
   */
  bindRegionChange: function (e) {
    console.log("f", e.detail.value);
    this.setData({
      province: e.detail.value[0],
      city: e.detail.value[1],
      region: e.detail.value[2],
      address: e.detail.value[0] + "," + e.detail.value[1] + "," + e.detail.value[2]
    })
  },
  getwxaddress() {
    let that = this;
    wx.chooseAddress({
      success(res) {
        console.log(res);
        that.setData({
          name: res.userName,
          phone: res.telNumber,
          province: res.provinceName,
          city: res.cityName,
          region: res.countyName,
          detail: res.detailInfo,
          address: res.provinceName + "," + res.cityName + "," + res.countyName
        })
      }
    })

  }

})